package com.lonely.fx.image;

import com.sun.prism.image.ViewPort;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

/**
 * @date: 2022-07-26 10:27
 * @author: ztkj-hzb
 * @description: ImageView 类的使用
 **/
public class ImageViewSettingMain extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    /**
     *
     */
    @Override
    public void start(Stage primaryStage) throws Exception {

        HBox hBox = new HBox();

        String path = "icons/shuidi.jpg";
        Image image = new Image(path);

        //创建ImageView对象
        ImageView imageView = new ImageView(image);

        //设置视图大小
        imageView.setFitWidth(600);
        imageView.setFitHeight(600);

        //设置等比例缩放
        imageView.setPreserveRatio(true);

        //获取试图宽高，注意，这里得宽高就是设置得宽高，即使设置了等比例，也还是返回的原先设置得宽高
        System.out.println(imageView.getFitWidth());
        System.out.println(imageView.getFitHeight());

        //获取真是视图的宽高
        System.out.println(imageView.prefWidth(-1));
        System.out.println(imageView.prefHeight(-1));


        //Rectangle2D rectangle2D = new Rectangle2D(0, 0, 200, 200);
        //imageView.setViewport(rectangle2D);

        //设置圆角
        Rectangle rectangle = new Rectangle(imageView.prefWidth(-1), imageView.prefHeight(-1));
        rectangle.setArcWidth(50);
        rectangle.setArcHeight(50);
        imageView.setClip(rectangle);


        //设置一个窗口展示指定区域的图片,这里做了一个根据鼠标移动的位置而展示对应的图片片段
        primaryStage.addEventHandler(MouseEvent.MOUSE_DRAGGED, new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                System.out.println("x：" + event.getX());
                System.out.println("screenX：" + event.getScreenX());
                System.out.println("sceneX：" + event.getSceneX());

                //设置设置
                Rectangle2D rectangle2D = new Rectangle2D(event.getX(), event.getY(), imageView.getFitWidth() / 2, imageView.prefHeight(-1) / 2);
                imageView.setViewport(rectangle2D);

            }
        });


        hBox.getChildren().addAll(imageView);
        primaryStage.setScene(new Scene(hBox));
        primaryStage.setWidth(1200);
        primaryStage.setHeight(900);
        primaryStage.show();
    }
}
